
//  ViewController.m
//  HDLiziAnimation
//
//  Created by mac on 2017/5/18.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ViewController.h"
//#import <QuartzCore/QuartzCore.h>

@interface ViewController ()
@property (nonatomic,strong)CAEmitterLayer *ringEmitter;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CGRect viewBounds = self.view.layer.bounds;
    
    //粒子发射器
    self.ringEmitter = [CAEmitterLayer layer];
    
    //粒子发射器的位置、大小
    self.ringEmitter.emitterPosition = CGPointMake(viewBounds.size.width/2.0, viewBounds.size.height/2.0);
    self.ringEmitter.emitterSize	= CGSizeMake(50, 0);
    
    //发射模式
    self.ringEmitter.emitterMode	= kCAEmitterLayerOutline;
    //发射源的形状
    self.ringEmitter.emitterShape	= kCAEmitterLayerCircle;
    //渲染模式
    self.ringEmitter.renderMode		= kCAEmitterLayerBackToFront;
    
    //粒子1
    CAEmitterCell* ring = [CAEmitterCell emitterCell];
    [ring setName:@"ring"];
    ring.birthRate			= 0;  //粒子的产生速率
    ring.velocity			= 250;  //粒子的速度
    ring.scale				= 0.5;  //粒子的缩放比例
    ring.scaleSpeed			=-0.2;  //粒子的缩放速度 ,正的代表放大，负的缩小
    /*粒子的RGB颜色值的变化速率，可以调节粒子的颜色变化*/
    ring.greenSpeed			=-0.2;
    ring.redSpeed			=-0.5;
    ring.blueSpeed			=-0.5;
    //粒子的生命周期
    ring.lifetime			= 2;
    //粒子的颜色
    ring.color = [[UIColor whiteColor] CGColor];
    //粒子的图片
    ring.contents = (id) [[UIImage imageNamed:@"DazTriangle"] CGImage];
    
    //粒子2
    CAEmitterCell* circle = [CAEmitterCell emitterCell];
    [circle setName:@"circle"];
    circle.birthRate		= 10;
    circle.emissionLongitude = M_PI * 0.5;
    circle.velocity			= 50;
    circle.scale			= 0.5;
    circle.scaleSpeed		=-0.2;
    circle.greenSpeed		=-0.1;
    circle.redSpeed			=-0.2;
    circle.blueSpeed		= 0.1;
    circle.alphaSpeed		=-0.2;
    circle.lifetime			= 4;
    circle.color = [[UIColor whiteColor] CGColor];
    circle.contents = (id) [[UIImage imageNamed:@"DazRing"] CGImage];
    
    //粒子3
    CAEmitterCell* star = [CAEmitterCell emitterCell];
    [star setName:@"star"];
    star.birthRate		= 10;
    star.velocity		= 100;
    star.zAcceleration  = -1;   //粒子z方向速度
    star.emissionLongitude = -M_PI; //发射方向
    star.scale			= 0.5;
    star.scaleSpeed		=-0.2;
    star.greenSpeed		=-0.1;
    star.redSpeed		= 0.4;	// shifting to red
    star.blueSpeed		=-0.1;
    star.alphaSpeed		=-0.2;
    star.lifetime		= 2;
    star.color = [[UIColor whiteColor] CGColor];
    star.contents = (id) [[UIImage imageNamed:@"DazStarOutline"] CGImage];
    
    self.ringEmitter.emitterCells = [NSArray arrayWithObject:ring];
//    ring.emitterCells = [NSArray arrayWithObjects:circle, star, nil];
    [self.view.layer addSublayer:self.ringEmitter];
}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    [self touchAtPosition:touchPoint];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    [self touchAtPosition:touchPoint];
}


- (void) touchAtPosition:(CGPoint)position
{
    CABasicAnimation *burst = [CABasicAnimation animationWithKeyPath:@"emitterCells.ring.birthRate"];
    burst.fromValue			= [NSNumber numberWithFloat: 100.0];
    burst.toValue			= [NSNumber numberWithFloat: 0.0];
    burst.duration			= 0.5;
    burst.timingFunction	= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    [self.ringEmitter addAnimation:burst forKey:@"burst"];
    
    [CATransaction begin];
    [CATransaction setDisableActions: YES];
    self.ringEmitter.emitterPosition	= position;
    [CATransaction commit];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
